home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / misc / math / TCalcStats2c.lha / TCalcStats2c / AREXX / Covariance.rexx < prev    next >
Encoding:
OS/2 REXX Batch file  |  1998-08-02  |  7.1 KB  |  341 lines

  1. /* Covariance Statistics */
  2.  
  3. options results
  4. if ~show('P','TCALC') then do
  5.     address command 'run turbocalc:turbocalc'
  6.     address command 'waitforport TCALC'
  7.     loadflag=1
  8. end
  9. address 'TCALC'
  10. 'DEFPUBSCREEN()'
  11. /* Add-in Rexx Math Library needed for some routines */
  12. signal on syntax
  13. if ~show('l','rexxmathlib.library') then
  14.    call addlib('rexxmathlib.library',0,-30)
  15. if ~show('l','rexxreqtools.library') then
  16.    call addlib('rexxreqtools.library',0,-30)
  17. if ~show('l','rexxsupport.library') then
  18.    call addlib('rexxsupport.library',0,-30)
  19.   /* add to library list */
  20. signal off syntax
  21.  
  22. /* Start Main Routine */
  23. if loadflag=1 then 'Load()'
  24. 'ActivateWindow()'
  25. range=rtgetstring(,"Enter Cell Range for Input","Input Request") /*,,'rt_pubscrname="TCALC"')*/
  26. colon=pos(":",range)
  27. if colon=0 then do
  28.     'Message "Please select a range before executing this script"'
  29.     'DEFPUBSCREEN("Workbench")'
  30.     exit
  31. end
  32.  
  33. /* Find cell references and cell, column numbers */
  34. start_cell=substr(range,1,colon-1)
  35. end_cell=substr(range,colon+1)
  36. start_row=cellrow(start_cell)
  37. end_row=cellrow(end_cell)
  38. start_col=cellcol(start_cell)
  39. end_col=cellcol(end_cell)
  40. NRows=end_row-start_row+1
  41. NCols=end_col-start_col+1
  42.  
  43. /* Get cell reference for output range */
  44. out_cell=rtgetstring(,"Enter Cell Reference for Output","Input Request") /*,,'rt_pubscrname="TCALC"')*/
  45. if out_cell="" then do
  46.     'DEFPUBSCREEN("Workbench")'
  47.     exit
  48. end
  49. if length(out_cell)<2 | datatype(left(out_cell,1),'n')=1 then do
  50.     'Message "Invalid cell reference"'
  51.     'DEFPUBSCREEN("Workbench")'
  52.     exit
  53. end
  54.  
  55.  
  56. /* Suppress Screen Redraw to Speed Things Up */
  57. 'Refresh 0'
  58.  
  59. /* Open a small output window on tcalc screen*/
  60. fo=0
  61. CR='0a'x
  62. DisplayMsg="Calculating...Please Wait."||CR||"User input is disabled during calculation."||CR
  63. if open(6Info, 'con:100/0/450/80/Progress/SCREEN TCALC', w) then do
  64.      call writeln(6Info, DisplayMsg)
  65.     fo=1
  66. end
  67. else do
  68.     'Message "TCALC Screen not available for Progress messages"'
  69. end
  70. CALL DELAY(150)
  71.  
  72. /* Get cell references for top cell in each column */
  73. 'SelectCell' start_cell
  74. do col=start_col to end_col
  75.     'GetCursorPos'
  76.     top_cell.col=result
  77.     'Column 1'
  78. end
  79.  
  80. /* Get labels for later use on output */
  81. 'SelectCell' start_cell
  82. 'GetValue'
  83. testlabel=result
  84. testlabel=strip(testlabel)
  85. if datatype(testlabel,'n')=1 then do
  86.     labelflag=0
  87.     do x=1 to NCols
  88.     title.x="Column "||x
  89.     end
  90. end
  91. else do
  92.     labelflag=1
  93.     NRows=NRows-1
  94.     do x=1 to NCols
  95.     'GetValue'
  96.     str=result
  97.     title.x=translate(strip(str),"_"," ")
  98.     'Column 1'
  99.     end
  100. end
  101. if fo then call writech(6Info,"Progress...10 ")
  102.  
  103. /* Get data from cell range */
  104. col=start_col
  105. lav=0
  106. tot=0
  107. count.=0
  108. total.=0
  109. do x=1 to NCols
  110.     'SelectCell' top_cell.col
  111.     if labelflag=1 then 'CursorDown 1'
  112.     do y=1 to NRows
  113.         'GetValue'
  114.         valtest=result
  115.         if datatype(valtest)='NUM' then do
  116.             'GetValue'
  117.             val=result
  118.             data.x.y=val
  119.             tot=tot+val
  120.             total.x=tot
  121.             count.x=1+count.x
  122.         end
  123.         'CursorDown 1'
  124.     end
  125.     col=col+1
  126.     tot=0
  127.     lav=0
  128.     val=0
  129. end
  130. if fo then call writech(6Info,"20 ")
  131.  
  132. /* Calculate Column Squares and Products */
  133. SQ.=0
  134. PR.=0
  135. TSQ.=0
  136. TPR.=0
  137. do x=1 to NCols
  138.     do y=1 to NRows
  139.         SQ.x=(data.x.y)**2
  140.         TSQ.x=(TSQ.x)+(SQ.x)
  141.     end
  142. end
  143. if fo then call writech(6Info,"40 ")
  144. do x=1 to NCols
  145.     do z=1 to NCols
  146.         do y=1 to NRows
  147.             PR.x.z=(data.x.y)*(data.z.y)
  148.             TPR.x.z=(TPR.x.z)+(PR.x.z)
  149.         end
  150.     end
  151. end
  152. N=count.1
  153. if fo then call writech(6Info,"60 ")
  154.  
  155. /* Calculate Means */
  156. mean.=0
  157. do x=1 to NCols
  158.     mean.x=(total.x)/(count.x)
  159. end
  160. if fo then call writech(6Info,"80 ")
  161.  
  162. /* Calculate Correlation */
  163. top.=0
  164. bot.=0
  165. cov.=0
  166. do x =1 to NCols
  167.     do z=1 to NCols
  168.         cov.x.z=((TPR.x.z)/N)-(mean.x)*(mean.z)
  169.     end
  170. end
  171. if fo then do
  172.     call writeln(6Info,"100 ")
  173.     call writeln(6Info,"Writing output to window...")
  174. end
  175.  
  176. /* Output */
  177. 'SelectCell' out_cell
  178. 'ColumnWidth 10'
  179. 'Put "Covariance: UnGrouped Data"'
  180. 'CursorDown 2'
  181. 'Column 1'
  182. do x=1 to NCols
  183.     'GetCursorPos'
  184.     first_cell.x=result
  185.     title=""""||title.x||""""
  186.     'Alignment 2'
  187.     'Put' title
  188.     'Column 1'
  189. end
  190. 'SelectCell' first_cell.1
  191. 'Column -1'
  192. 'CursorDown 1'
  193. do x=1 to NCols
  194.     title=""""||title.x||""""
  195.     'Put' title
  196.     'CursorDown 1'
  197. end
  198. d=0
  199. b=0
  200. do x=1 to NCols
  201. 'SelectCell' first_cell.x
  202. 'CursorDown 1'
  203.     do z=1 to NCols
  204.         'Put' format(cov.x.z,,4)
  205.         'CursorDown 1'
  206.     end
  207. end
  208. 'Refresh 1'
  209. 'Refresh 2'
  210. /*'Message' "Finished"*/
  211. /*indicate the main script is finished*/
  212. DisplayMsg="Cleaning up ...."||CR||"Exiting"
  213. result=writeln(6Info, DisplayMsg)
  214. if result~=0 then do
  215.     /*Wait 3 seconds*/
  216.     CALL DELAY(150)
  217.     /* close window*/
  218.     result=close(6Info)
  219. end
  220. 'DEFPUBSCREEN("Workbench")'
  221. exit
  222.  
  223. /* Procedures */
  224.  
  225. cellrow: procedure
  226. do
  227.     parse arg cell
  228.     do charpos=2 to length(cell)
  229.     if datatype(substr(cell,charpos,1),n) then return substr(cell,charpos)
  230.     end
  231.     return 0
  232. end
  233. Return
  234.  
  235. cellcol: procedure
  236. do
  237.     parse arg cell
  238.     labels="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  239.     cell=upper(cell)
  240.     len=length(cell)
  241.     val=0
  242. do charpos=1 to len
  243.     if datatype(substr(cell,charpos,1),n) then
  244.     do cell=reverse(substr(cell,1,charpos-1))
  245.     do x=1 to length(cell)
  246.     val=(26**(x-1))*pos(substr(cell,x,1),labels)+val
  247.     end
  248.     return val
  249.     end
  250.     end
  251.     return 0
  252. end
  253. Return
  254. /* It is important to put the exposed array at the end of the next line */
  255. Sort: procedure expose NCols NRows data.
  256. do x=1 to NCols
  257. L=(xtoy(2,int(log(NRows-1)/log(2))))-1
  258.     Do Until L<1
  259.     L=trunc(int(L/2))
  260.     Do J=1 to L
  261.             Do K=J+L To NRows-1 By L
  262.             I=K
  263.             dumdat=data.x.I
  264.             Do while I>L
  265.                 y=I-L
  266.                 If data.x.y ~> dumdat then Leave
  267.                 data.x.I=data.x.y
  268.                 I=I-L
  269.             End
  270.             data.x.I=dumdat
  271.             End
  272.         End
  273.     End
  274. End
  275. Return
  276.  
  277. syntax:
  278.      if arg(1)='FAIL' then do
  279.         'Message "Library is unavailable."'
  280.         'DEFPUBSCREEN("Workbench")'
  281.         exit
  282.         end
  283.     'DEFPUBSCREEN("Workbench")'
  284.     exit
  285.  
  286. Format:  procedure
  287.  
  288.      arg number, before, after
  289.      CallLine = SIGL
  290.      if ~datatype(CallLine, 'N') then CallLine = '??'
  291.  
  292.      /* Make sure we have a number as first (required) argument    */
  293.      if ~datatype(number, 'N') then do
  294.         if number = '' then
  295.            fc = 17     /* Wrong number of arguments           */
  296.         else
  297.            fc = 47     /* Arithmetic conversion error             */
  298.         signal FormatSyntaxError
  299.      end
  300.      num = number + 0
  301.      if before = '' & after = '' then
  302.         return num
  303.      else do
  304.         parse var num integer '.' fraction
  305.         if before = '' then before = length(integer)
  306.         if after = '' then after = length(fraction)
  307.         if ~datatype(before, N) | ~datatype(after, N) then
  308.            do fc = 18
  309.            signal FormatSyntaxError
  310.        end
  311.         if before < length(integer) then do
  312.            fc = 18
  313.            signal FormatSyntaxError
  314.         end
  315.         if after ~= length(fraction) then do
  316.            fraction = trunc(('.'fraction'0') + ('.'copies('0', after)'5'), after)
  317.         if integer<1&integer>-1 then integer=integer
  318.            else integer = integer + (fraction % 1)
  319.            fraction = substr(fraction, 3)
  320.         end
  321.         if fraction >= 0 then
  322.            return right(integer, before)'.'fraction
  323.         else
  324.            return right(integer, before)
  325.      end
  326.  
  327.  FormatSyntaxError:
  328.         if show('F', STDERR) then
  329.            call writeln(STDERR, '+++ Error' fc 'in line' CallLine':' errortext(fc))
  330.         else
  331.            mess='+++ Error' fc 'in line' CallLine':' errortext(fc)
  332.         'Message' mess
  333.         parse source Func .
  334.         if Func = 'FUNCTION' then do
  335.         'DEFPUBSCREEN("Workbench")'
  336.            exit "Err"
  337.         end
  338.         else do
  339.         'DEFPUBSCREEN("Workbench")'
  340.            exit 10
  341.         end